/**
 * 
 */
package com.application.DataProvider;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.application.DTO.FeedbackDTO;

/**
 * @author TAI
 *
 */
public class FeedbackDAO {

	Context _mcontext = null;

	public FeedbackDAO(Context context){
		this._mcontext = context;
	}

	/**
	 * Kiểm tra Feedback tồn tại
	 * @param id
	 * @return
	 */
	public boolean isExist(int id)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		boolean result = false;
		Cursor cur = null;
		try 
		{
			String sql = "Select count(*) from Feedback where ID=" +  id;
			cur = provider.execQuery(db, sql);
			while (cur.moveToNext()) {
				int i = Integer.valueOf(cur.getString(0));
				if(i != 0)
					result = true;
			}
		} catch (Exception e) {
			// TODO: handle exception
		} finally{
			cur.close();
			db.close();
		}
		return result;
	}
	/**
	 * Insert Feedback
	 * @param fb_dto
	 * @return
	 */
	public int insertFeedback(FeedbackDTO fb_dto) {
		Log.d("PEERDROID","insert Feedback");
		int result = 0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try {
			ContentValues values = new ContentValues();
			values.put("ID", fb_dto.get_ID());
			values.put("IDDetailLearning", fb_dto.get_IDDetailLearning());
			values.put("IDLearner", fb_dto.get_IDLearner());
			values.put("IDQuestion", fb_dto.get_IDQuestion());
			values.put("Content", fb_dto.get_Content());
			values.put("Answer",fb_dto.get_Answer());

			result = provider.execInsertCommand(db, "Feedback", values);
		} catch (Exception e) {
			// TODO: handle exception
		} finally {
			db.close();
		}

		return result;
	}

	/**
	 * Update Feedback
	 * @param fb_dto
	 * @return
	 */
	public int updateFeedback(FeedbackDTO fb_dto)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{
			ContentValues values = new ContentValues();
			values.put("IDDetailLearning", fb_dto.get_IDDetailLearning());
			values.put("IDLearner", fb_dto.get_IDLearner());
			values.put("IDQuestion", fb_dto.get_IDQuestion());
			values.put("Content", fb_dto.get_Content());
			values.put("Answer",fb_dto.get_Answer());

			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(fb_dto.get_ID())};
			result= provider.execUpdateCommand(db, "Feedback", values, whereClause, whereArgs);

		} catch (Exception e) {
			// TODO: handle exception
		}finally {
			db.close();
		}

		return result;
	}
	/**
	 * Delete Feedback
	 * @param idFeedback
	 * @return
	 */
	public int deleteFeedback(int idFeedback)
	{
		int result=0;
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		try{	
			String whereClause="ID=?";
			String[]whereArgs = {String.valueOf(idFeedback)};	
			result=provider.execDeleteCommand(db, "Feedback", whereClause, whereArgs);
		} catch (Exception e) {
			// TODO: handle exception
		}
		finally {
			db.close();
		}
		return result;
	}
	public boolean KiemTraBaiTapTonTai(int iddetaillearning, int idquestion, String nameex, int idlearner)
	{
		DataProvider provider = new DataProvider(_mcontext);
		SQLiteDatabase db = provider.openDataBase();
		Cursor cur = null;
		String sql ="select count(*) as 'count' from detaillearning d, feedback f " +
			" where d.id = f.iddetaillearning and f.idquestion = "+ idquestion +" and f.idlearner = "+ idlearner +" and f.iddetaillearning = "+ iddetaillearning +" and f.content = '" + nameex + "'";
		
		try {
			cur = provider.execQuery(db, sql);
			while(cur.moveToNext())
			{
				if(cur.getInt(0) != 0)
					return true;
			}
		} catch (Exception e) {
			Log.d("PEERDROID","Loi ham KiemTraBaiTapTontai " + e.toString());
		}
		finally
		{
			cur.close();
			db.close();
		}
		return false;
	}
}
